home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2000 / MacHack 2000.toast / pc / The Hacks / FinderGrok / Grok / Source / SelectFile.cp < prev    next >
Text File  |  2000-06-23  |  5KB  |  264 lines

  1. //
  2. //  C++ Toolbox Stationery
  3. //  by Josef W. Wankerl
  4. //  04/11/00
  5. //
  6.  
  7. #include <Dialogs.h>
  8. #include <Fonts.h>
  9. #include <MacWindows.h>
  10. #include <Menus.h>
  11. #include <QuickDraw.h>
  12. #include <TextEdit.h>
  13. #include <string.h>
  14. #include <ctype.h>
  15. #include "SelectFile.h"
  16.  
  17. SelectFile::SelectFile()
  18. {
  19.     editrec = NULL;
  20.     TheString = NULL;
  21.     Initialize();
  22. }
  23.  
  24. void SelectFile::Initialize()
  25. {
  26. /*    InitGraf(&qd.thePort);
  27.     InitFonts();
  28.     InitFloatingWindows();
  29.     InitMenus();
  30.     TEInit();
  31.     InitDialogs(nil);
  32.     InitCursor();
  33. */
  34. }
  35.  
  36. void SelectFile::MyEventLoop()
  37. {
  38.     RgnHandle    cursorRgn = NewRgn();
  39.     Boolean      gotEvent = false;
  40.     EventRecord  event;
  41.     bool         Stop = false;    
  42.     
  43.     TEActivate(editrec);
  44.         
  45.     while( !Stop )
  46.     {
  47.         gotEvent = WaitNextEvent(everyEvent, &event, 1, cursorRgn);
  48.         if(gotEvent)
  49.         {
  50.             switch(event.what)
  51.             {
  52.                 case updateEvt:
  53.                     Stop = DoUpdate(&event);
  54.                     break;
  55.  
  56.                 case keyDown:
  57.                 case autoKey:
  58.                     Stop = DoKeyDown(&event);
  59.                        break;                 
  60.  
  61.                    case mouseDown:
  62.                     Stop = DoMouseDown(&event);
  63.                        break;
  64.  
  65.                    default:
  66.                        Stop = DoIdle(&event);
  67.                        // do idle
  68.  
  69.             }; // case
  70.                         
  71.         };
  72.     };
  73.     
  74.     TEDeactivate(editrec);
  75.     
  76. };
  77.  
  78. void SelectFile::Run()
  79. {
  80.     WindowPtr myWindow;
  81.     Rect      theRect;
  82.     OSStatus  err;
  83.  
  84.     SetRect( &theRect, 40, 40, 400, 80);
  85.  
  86.     // create the main window.
  87.     err = CreateNewWindow(kFloatingWindowClass, kWindowStandardFloatingAttributes, &theRect, &myWindow);
  88.     err = TransitionWindow(myWindow, kWindowZoomTransitionEffect, kWindowShowTransitionAction, &theRect);
  89.     ShowFloatingWindows();
  90.     SetPort(myWindow);
  91.     
  92.     // create the Text Edit
  93.     SetRect( &theRect, 0, 0, 360, 40);
  94.     editrec = TENew(&theRect, &theRect);
  95.  
  96.     // Loop 'till your hearts content
  97.     MyEventLoop();
  98.  
  99.     // clean up
  100.     TEDispose(editrec);
  101.     CloseWindow(myWindow);
  102.     DisposeWindow(myWindow);
  103. }
  104.  
  105. bool SelectFile::DoUpdate(EventRecord* event)
  106. {
  107.     Rect theRect;
  108.  
  109.     theRect.top = 0;
  110.     theRect.left = 0;
  111.     theRect.bottom = 40;
  112.     theRect.right = 360;
  113.     
  114.     BeginUpdate((WindowPtr)event->message);
  115.     SetPort((WindowPtr)event->message);
  116.     EraseRect(&theRect);
  117.     TEUpdate(&theRect, editrec);
  118.     EndUpdate((WindowPtr)event->message);
  119.  
  120.     return false;
  121. } // DoUpdate
  122.  
  123.  
  124. bool SelectFile::DoKeyDown(EventRecord*  event)
  125. {
  126.     if( (char)event->message != '\r')
  127.     {
  128.         TEKey((char)event->message, editrec);
  129.         return false;
  130.     }
  131.     else
  132.     {        
  133.         CharsHandle  hTextString = TEGetText(editrec);
  134.         unsigned char state = HGetState(hTextString);
  135.         HLock(hTextString);
  136.         unsigned long size = (*editrec)->teLength;
  137.         
  138.         if(TheString != NULL) DisposePtr(TheString);
  139.         TheString = NewPtr(size + 1);
  140.         strncpy(TheString, *hTextString, size);
  141.         TheString[size] = '\0';
  142.         
  143.         HSetState(hTextString, state);
  144.         
  145.         return true;
  146.     }
  147.  
  148. } // DoKeyDown
  149.  
  150.  
  151. bool SelectFile::DoMouseDown(EventRecord*  event)
  152. {
  153.     TEClick((Point)event->where,
  154.             ((event->modifiers & shiftKey)!=0),
  155.             editrec);
  156.     return false;
  157. } // DoMouseDown
  158.  
  159.  
  160. bool SelectFile::DoIdle(EventRecord*  /*event*/)
  161. {
  162.     TEIdle(editrec);
  163.     return false;
  164. } // DoIdle
  165.  
  166. bool SelectFile::PaternMatch(char* FileName)
  167. {
  168.     // just call the aux version
  169.     return PaternMatchAux(FileName, TheString);
  170. } // PaternMatch
  171.  
  172. bool SelectFile::PaternMatch(Str255 FileName)
  173. {
  174.     // find the length of the pstring and create 
  175.     // space in the heap for a copy
  176.     char SZ = FileName[0];
  177.     char* cFN = new char [ (unsigned long)(SZ+1) ];
  178.     
  179.     // copy the memory and set the terminator to \0
  180.     memcpy(cFN, &FileName[1], SZ);
  181.     cFN[SZ] = '\0';
  182.     
  183.     // cal the ascz verson
  184.     bool lval = PaternMatch( cFN );
  185.     
  186.     // clean up and return
  187.     delete cFN;
  188.     return lval;
  189.     
  190. } // PaternMatch pascal versin
  191.  
  192. bool SelectFile::PaternMatchAux(char* FileName, char* Pat)
  193. {
  194.     ThreeState Match = maby;    
  195.     
  196.     // loop through each string 
  197.     while( Match == maby )
  198.     {
  199.         switch(*Pat)
  200.         {
  201.             case '?':
  202.                 // match one of any char.
  203.                 Pat++;
  204.                 if( *FileName != '\0')
  205.                     FileName++;
  206.                 else
  207.                     Match = no;
  208.                 break;
  209.  
  210.             case '*':
  211.                 // match 0 or more any char
  212.                 while((*Pat == '*') || (*Pat == '?')) 
  213.                     {
  214.                         if( (*Pat == '?' ) && (*FileName != '\0') )
  215.                             FileName++;
  216.                             
  217.                         Pat++;
  218.                     }
  219.                     
  220.                 while( (*FileName != '\0') && (tolower(*FileName) != tolower(*Pat)) )
  221.                     ++FileName;
  222.                         
  223.  
  224.                 Match = ( PaternMatchAux(FileName, Pat) ? yes : no );
  225.  
  226.                 while( (Match != yes) && (*FileName != '\0') )
  227.                     Match = ( PaternMatchAux(++FileName, Pat) ? yes : no );
  228.                 break;
  229.                 
  230.             case '\0':
  231.                 // we've reached the end of the pattern
  232.                 Match = ((tolower(*FileName) == tolower(*Pat)) ? yes : no);
  233.                 break;
  234.  
  235.             default:
  236.                 // this is a normal char
  237.                 if( tolower(*FileName) == tolower(*Pat) )
  238.                 {
  239.                     FileName++;
  240.                     Pat++;
  241.                 }
  242.                 else
  243.                     Match = no;
  244.                 
  245.         }; // switch
  246.     }; // while
  247.     
  248.     return( Match == yes );
  249. }; // PaternMatch
  250.  
  251. #ifdef    NEVER
  252. void main(void)
  253. {
  254.     SelectFile theApplication;
  255.     
  256.     theApplication.Run();
  257.     
  258.     bool test = theApplication.PaternMatch("\pbob.txt");
  259.     
  260.     test = !test;
  261.     
  262.     // shane, theApplication.TheString contains the string entered by the user.
  263. }
  264. #endif